package com.haixee.parkdemo.data_acl.mybatis;

import com.haixee.parkdemo.data_acl.mybatis.service.DataAclService;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Properties;

@Configuration
//@ConditionalOnBean(SqlSessionFactory.class)
@AutoConfigureAfter(MybatisAutoConfiguration.class)
public class DataAclConfiguration {

    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;

    @Autowired
    private DataAclService dataAclService;

    @PostConstruct
    public void addDataAclInterceptor() {
        DataAclInterceptor interceptor = new DataAclInterceptor();
        Properties properties = new Properties();

//        properties.putAll(this)
        interceptor.setProperties(properties);
        interceptor.setDataAclService(dataAclService);
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
        }
    }
}
